#include "EditorEditableTextViewModel.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

#include "Font/FontSubsystem.h"

REditorEditableTextViewModel::REditorEditableTextViewModel()
{
    Font=REditor::Get()->CurrentTheme->DefaultFont;
}

void REditorEditableTextViewModel::OnMuseButtonClicked(float MouseRelativeX)
{ 
    //calculate the position of the cursor
    BeamPosition=RFontApplicationSubsystem::GetInstance()->CalculateCursorPosition(DisplayText,Font,FontSize,MouseRelativeX);
    
    if(!IsEditing())
    {
        StartEditing();
    }

}

bool REditorEditableTextViewModel::OnTextInput(const STextInputEvent &InEvent)
{

    CString InputText=InEvent.GetInputText();
    DisplayText=DisplayText.SubString(0,BeamPosition.CharacterIndex)+InputText+DisplayText.SubString(BeamPosition.CharacterIndex);
    BeamPosition.CharacterIndex=BeamPosition.CharacterIndex+InputText.GetLength();
    BeamPosition.X=RFontApplicationSubsystem::GetInstance()->GetOccupiedSize(DisplayText.SubString(0,BeamPosition.CharacterIndex),Font,FontSize).X;

    OnDisplayTextChanged.Broadcast();

    CursorShowState = true;
    ElapsedTime = 0.0f;
    return true;    
}

bool REditorEditableTextViewModel::OnKeyboardEvent(const SKeyboardEvent &InKeyboardEvent)
{        

    if(InKeyboardEvent.KeyState!=EKeyState::Pressed)
    {
        return false;
    }

    switch (InKeyboardEvent.Key)
    {
        case EKey::Left:
        {
            if(BeamPosition.CharacterIndex>0)
            {
                BeamPosition.CharacterIndex--;
                BeamPosition.X=RFontApplicationSubsystem::GetInstance()->GetOccupiedSize(DisplayText.SubString(0,BeamPosition.CharacterIndex),Font,FontSize).X;
            }
            break;
        }

        case EKey::Right:
        {
            if(BeamPosition.CharacterIndex<DisplayText.GetLength())
            {
                BeamPosition.CharacterIndex++;
                BeamPosition.X=RFontApplicationSubsystem::GetInstance()->GetOccupiedSize(DisplayText.SubString(0,BeamPosition.CharacterIndex),Font,FontSize).X;
            }
            break;
        }

        case EKey::Backspace:
        {
            if(BeamPosition.CharacterIndex>0)
            {
                DisplayText=DisplayText.SubString(0,BeamPosition.CharacterIndex-1)+DisplayText.SubString(BeamPosition.CharacterIndex);
                BeamPosition.CharacterIndex--;
                BeamPosition.X=RFontApplicationSubsystem::GetInstance()->GetOccupiedSize(DisplayText.SubString(0,BeamPosition.CharacterIndex),Font,FontSize).X;

                OnDisplayTextChanged.Broadcast();
            }
            break;
        }

        case EKey::Enter:
        {
            EndEditing(EReasonForEndEditing::Confirm);
            break;
        }
    }

    CursorShowState = true;
    ElapsedTime = 0.0f;
    return true;
}

void REditorEditableTextViewModel::Tick(float DeltaSeconds)
{
    if(!IsEditing())
    {
        return;
    }

    ElapsedTime += DeltaSeconds;
    if(ElapsedTime>0.5f)
    {
        CursorShowState = !CursorShowState;
        ElapsedTime = 0.0f;
    }
}
